/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/
#include "IncFreeTrack.h"
#include "freetrackdialog.h"
#include "freetrack.h"

#include "aboutdialog.h"
#include "configwidget.h"

#include "layermanager.h"
#include "trackgps.h"
#include "tracktable.h"
#include "mainselector.h"
#include "statusbar.h"

FreeTrack* freetrackObject;

FreeTrack::FreeTrack(QWidget *parent) :
    QMainWindow(parent),
    config(this)
{
    freetrackObject = this;
    // configure widget: make sure window is not transparent (bug on symbian) and
    // no contect menu is assigned (another bug on symbian)
    setBackgroundRole( QPalette::Window );
    setContextMenuPolicy(Qt::NoContextMenu);

    // Quit when last windows has been closed
    connect( qApp, SIGNAL(lastWindowClosed()),
             qApp, SLOT(quit()));

    // Table, GPS and LayerManager are inside a QStackedWidget:
    center = new QStackedWidget( this );

    // Create the layers manager
    layers = new LayerManager(this);
    center->addWidget( layers );

    // Create the GPS page
    gps = new TrackGps(this);
    center->addWidget( gps );

    // Create the tracking table, which is the default opened widget
    last_displayed = table = new TrackTable(this, layers);
    center->addWidget( table );
    // hack to inseminate current initial position to table (will emit a signal)
    gps->postInitialize();

    // Create the selector page
    selector = new MainSelector();
    center->addWidget( selector );
    // set as default widget
    center->setCurrentWidget( selector );

    // Create the status bar
    statusBar = new StatusBar();

    // Build FreeTrack layout...
    QVBoxLayout* grid = new QVBoxLayout();
    grid->setSizeConstraint( QLayout::SetMinAndMaxSize );
    grid->setContentsMargins(0,0,0,0);

    setMaximumSize(QApplication::desktop()->screenGeometry().size());

    grid->setSpacing(0);
    grid->setAlignment( Qt::AlignLeft | Qt::AlignTop );
    // Add center widget
    grid->addWidget(center, 2);
    // Add status-bar
    grid->addWidget( statusBar );

    // Create the central widget of the MainWindoe
    setCentralWidget( new QWidget());
    centralWidget()->setLayout( grid );
}

FreeTrack::~FreeTrack()
{  
}

void FreeTrack::screenRotated(int)
{
}

void FreeTrack::showLayers()
{
    last_displayed = layers;
    center->setCurrentWidget( layers );
    statusBar->uncheck();
}

void FreeTrack::showTable()
{
    last_displayed = table;
    center->setCurrentWidget( table );
    statusBar->uncheck();
}

void FreeTrack::showSelector(bool open_selector)
{
    if ( open_selector )
        center->setCurrentWidget( selector );
    else
        center->setCurrentWidget( last_displayed );
}

void FreeTrack::showGps()
{
    last_displayed = gps;
    center->setCurrentWidget( gps );
    statusBar->uncheck();
}

void FreeTrack::showAbout()
{
    FreeTrackDialog* dialog = new FreeTrackDialog( new AboutDialog() );
    dialog->exec();
    delete dialog;
}

void FreeTrack::showConfig()
{
    FreeTrackDialog* dialog = new FreeTrackDialog( &config );
    dialog->exec();
    // We re-parent the config widget or it will be destroyed!
    config.setParent(this);
    delete dialog;
}

void FreeTrack::hideMe()
{
#ifdef Q_OS_SYMBIAN
    // On Symbian, we need to "lower" the window
    lower();
#else
    // Otherwise, we can safely minimize it
    showMinimized();
#endif
}

void FreeTrack::askQuit()
{
    if ( QMessageBox::question( this,
                                tr("Quit Freetrack..."),
                                tr("Really close?"),
                                QMessageBox::Yes|QMessageBox::No,
                                QMessageBox::NoButton ) == QMessageBox::Yes )
        qApp->exit(0);
}

void FreeTrack::resizeEvent(QResizeEvent* ev)
{
    QSize ss = QApplication::desktop()->screenGeometry().size();
    setMinimumSize( ss );
    setMaximumSize( ss );
    showFullScreen();
    QMainWindow::resizeEvent( ev );
}
